<!DOCTYPE html>
<html>
<!--
Copyright 2011 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>goog.editor.plugins.LinkDialogPlugin Tests</title>
<script src="../../base.js"></script>
<script>
  goog.require('goog.dom.DomHelper');
  goog.require('goog.editor.BrowserFeature');
  goog.require('goog.editor.Command');
  goog.require('goog.editor.Field');
  goog.require('goog.editor.node');
  goog.require('goog.editor.plugins.EquationEditorPlugin');
  goog.require('goog.ui.editor.EquationEditorDialog');
  goog.require('goog.ui.editor.EquationEditorOkEvent');
  goog.require('goog.ui.equation.EquationEditorDialog');
  goog.require('goog.testing.MockControl');
  goog.require('goog.testing.MockRange');
  goog.require('goog.testing.editor.FieldMock');
  goog.require('goog.testing.editor.TestHelper');
  goog.require('goog.testing.editor.dom');
  goog.require('goog.testing.events');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.mockmatchers');
  goog.require('goog.ui.editor.AbstractDialog.EventType');
  goog.require('goog.ui.editor.LinkDialog');
  goog.require('goog.ui.editor.LinkDialog.OkEvent');
</script>
<link rel="stylesheet" href="../../css/dialog.css"/>
<link rel="stylesheet" href="../../css/editor/dialog.css"/>
<link rel="stylesheet" href="../../css/editor/linkdialog.css"/>
</head>
<body>
<script>
  var plugin;
  var imgElem;

  var mockCtrl;
  var mockRange;
  var mockField;
  var mockEditor;
  var mockPlaceCursorNextTo;
  var propertyReplacer = new goog.testing.PropertyReplacer();
  var fieldObj;  
  var fieldElem;
  var domHelper = new goog.dom.DomHelper();
  var IMG_URL = 'https://www.google.com/chart?cht=tx&chf=bg,s,' +
                'FFFFFF00&chco=000000&chl=x^2%2Fy^2';

  function setUp() {
    mockCtrl = new goog.testing.MockControl();
    mockRange = new goog.testing.MockRange();
    mockCtrl.addMock(mockRange);
    mockField = new goog.testing.editor.FieldMock(undefined, undefined,
                                                  mockRange);
    mockCtrl.addMock(mockField);
    mockPlaceCursorNextTo = mockCtrl.createFunctionMock('placeCursorNextTo');
  }

  function tearDown() {
    plugin.dispose();
  }
  
  /**
   * Sets up expectations for testing when the OK event fires: the field is
   * focused, the image is inserted.
   * @param {string} imageUrl Url of the image being inserted.
   */
  function setUpTestOk(imageUrl) {
    mockField.focus().$times(1);
    var tempImage = goog.dom.createDom(goog.dom.TagName.IMG, {src: imageUrl});
    var fullImageUrl = tempImage.src;
    var createdNodeMatcher = new goog.testing.mockmatchers.ArgumentMatcher(
          function(arg) {
            if (!arg) {
              return true;
            }
            return arg.tagName == goog.dom.TagName.IMG &&
                   arg.src == fullImageUrl;
          },
          '<IMG src="' + imageUrl + '">');
    mockPlaceCursorNextTo(createdNodeMatcher, false);
    mockRange.isCollapsed().$times(1);
    mockRange.removeContents().$times(1);
    mockRange.insertNode(
        domHelper.htmlToDocumentFragment(imageUrl), false).$times(1);
    propertyReplacer.set(goog.editor.range,
                         'placeCursorNextTo',
                         mockPlaceCursorNextTo);
    mockField.dispatchChange().$times(1);
  }
  
  /**
   * Tests that when the OK event fires the editable field is properly updated.
   */
  function testOk() {
    setUpTestOk(IMG_URL);
    mockField.dispatchBeforeChange().$times(1);
    mockCtrl.$replayAll();

    plugin = new goog.editor.plugins.EquationEditorPlugin();
    plugin.registerFieldObject(mockField);
    var dialog = plugin.createDialog(domHelper);
    // Mock of execCommand + clicking OK without actually opening the dialog.
    dialog.dispatchEvent(
        new goog.ui.editor.EquationEditorOkEvent(IMG_URL));
    mockCtrl.$verifyAll();
  }

</script>
</body>
</html>
